package cn.yulr.multicamera;

import android.hardware.Camera;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Button;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    private Camera frontCamera;
    private Camera backCamera;

    private SurfaceView camera01SurfaceView;
    private SurfaceView camera02SurfaceView;

    private Button startCamera01Button;
    private Button startCamera02Button;
    private Button startCameraBButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        camera01SurfaceView = findViewById(R.id.camera01SurfaceView);
        camera02SurfaceView = findViewById(R.id.camera02SurfaceView);

        startCamera01Button = findViewById(R.id.startCamera01Button);
        startCamera02Button = findViewById(R.id.startCamera02Button);
        startCameraBButton = findViewById(R.id.startCameraBButton);

        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        int cameraCount = Camera.getNumberOfCameras();
        Log.e("获取到摄像头的数量为：" + cameraCount);

//        backCamera = Camera.open(0);
        frontCamera = Camera.open(1);

        camera02SurfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder surfaceHolder) {

                if (frontCamera != null) {
                    Camera.Parameters parameters = frontCamera.getParameters();
                    parameters.setPreviewSize(640, 480);
//                            * @see #FOCUS_MODE_AUTO
//                            * @see #FOCUS_MODE_INFINITY
//                            * @see #FOCUS_MODE_MACRO
//                            * @see #FOCUS_MODE_FIXED
//                            * @see #FOCUS_MODE_EDOF
//                            * @see #FOCUS_MODE_CONTINUOUS_VIDEO
//                    parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_FIXED);
                    frontCamera.setParameters(parameters);
                    try {
                        frontCamera.setPreviewDisplay(camera02SurfaceView.getHolder());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    frontCamera.setPreviewCallback(new Camera.PreviewCallback() {
                        int count = 1;

                        @Override
                        public void onPreviewFrame(byte[] data, Camera camera) {
                            if ((count++) % 300 == 0) {
                                count = 1;
                                Log.e("前置摄像头获取数据");
                            }
                        }
                    });
                    frontCamera.startPreview();

                }

            }

            @Override
            public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder surfaceHolder) {

            }
        });

//        camera02SurfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
//            @Override
//            public void surfaceCreated(SurfaceHolder surfaceHolder) {
//                try {
//                    TimeUnit.SECONDS.sleep(3L);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//
//                Log.e("启动第二摄像头");
//
//                if (backCamera != null) {
//                    Camera.Parameters parameters = backCamera.getParameters();
//                    parameters.setPreviewSize(640, 480);
//                    backCamera.setParameters(parameters);
//                    try {
//                        backCamera.setPreviewDisplay(camera02SurfaceView.getHolder());
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
//                    backCamera.setPreviewCallback(new Camera.PreviewCallback() {
//                        int count = 0;
//
//                        @Override
//                        public void onPreviewFrame(byte[] data, Camera camera) {
//                            if ((count++) % 300 == 0) {
//                                count = 0;
//                                Log.e("后置摄像头获取数据");
//                            }
//                        }
//                    });
//                    backCamera.startPreview();
//                }
//            }
//
//            @Override
//            public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
//
//            }
//
//            @Override
//            public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
//
//            }
//        });

    }

    @Override
    protected void onStop() {
        super.onStop();

        if (this.frontCamera != null) {
            this.frontCamera.stopPreview();
            this.frontCamera.release();
            this.frontCamera = null;
        }

        if (this.backCamera != null) {
            this.backCamera.stopPreview();
            this.backCamera.release();
            this.backCamera = null;
        }


    }
}
